<?
include '../includes/specs_functions.php';

#----------------------------------------------------
# Connect to PADS
$DB = getDB();
$SESSION = get_session($DB);
$THEME = $SESSION->Theme;

$jobId = $_POST['jobId'];
$level = $_POST['level'];
$wide = $_POST['wide'];

$w = "=";
if($wide=="true"){
	$w = "<=";
}

$table = "(((SELECT  co.".SPEC_TBL_COMPONENTS_SKILL_ID.", COUNT(*) as skillCount 
					FROM (".SPEC_TBL_COMPONENTS." as co
					LEFT JOIN ".SPEC_TBL_SPEC." as spec
					ON spec.".SPEC_TBL_SPEC_ID." = co.".SPEC_TBL_COMPONENTS_SPEC_ID.")
					WHERE spec.".SPEC_TBL_SPEC_JOB_ID." = \"".$jobId."\" 
					AND spec.".SPEC_TBL_SPEC_LEVEL." ".$w." \"".$level."\" 
					AND co.".SPEC_TBL_COMPONENTS_TEMPLATE." = \"N\"
					GROUP BY co.".SPEC_TBL_COMPONENTS_SKILL_ID.")  as comp
		LEFT JOIN
		".TBL_ABILITIES." as ab
		ON comp.".SPEC_TBL_COMPONENTS_SKILL_ID. "= ab.".TBL_ABILITIES_ID.")
		LEFT JOIN ".SPEC_TBL_JOBS." as job 
		ON job.".SPEC_TBL_JOBS_ID." = ab.".TBL_ABILITIES_CLASS.")";
$col = array(
		"skillCount"
		,"job.".SPEC_TBL_JOBS_NAME." as skillName"
		,"ab.".TBL_ABILITIES_ID." as id"
		,"ab.".TBL_ABILITIES_ENNAME." as name"
		,"ab.".TBL_ABILITIES_LEVEL." as level"
		,"ab.".TBL_ABILITIES_MP." as mp"
		,"ab.".TBL_ABILITIES_HP." as hp"
		,"ab.".TBL_ABILITIES_TP." as tp"
		,"ab.".TBL_ABILITIES_CASTTIME." as cast"
		,"ab.".TBL_ABILITIES_RECASTTIME." as recast"
		,"ab.".TBL_ABILITIES_RANGE." as range"
		,"ab.".TBL_ABILITIES_ICON." as icon"
		,"ab.".TBL_ABILITIES_ENDESC." as description"
		);

$orderBy = "skillCount";
$direction = "DESC";
$limit = array( "0", "10");

$result = $DB->GetData($table ,$col ,true ,null, $orderBy, $direction, $limit);
//print_r($result);


$tCountabfrage = "(SELECT COUNT(*) as totalCount 
					FROM (".SPEC_TBL_COMPONENTS." as co
					LEFT JOIN ".SPEC_TBL_SPEC." as spec
					ON spec.".SPEC_TBL_SPEC_ID." = co.".SPEC_TBL_COMPONENTS_SPEC_ID.")
					WHERE spec.".SPEC_TBL_SPEC_JOB_ID." = \"".$jobId."\" 
					AND spec.".SPEC_TBL_SPEC_LEVEL." = \"".$level."\" 
					AND co.".SPEC_TBL_COMPONENTS_TEMPLATE." = \"N\")";

$tCountresult = $DB->RawQuery($tCountabfrage);
/*
$jNameafrage = "(SELECT ".SPEC_TBL_JOBS_NAME." as jobName 
					FROM ".SPEC_TBL_JOBS." 
					WHERE ".SPEC_TBL_JOBS_ID." = \"".$jobId."\")";

$jNameresult = $DB->RawQuery($jNameafrage);

//print_r($result);

$abfrage = "SELECT  co.".SPEC_TBL_COMPONENTS_SKILL_ID.", COUNT(*) as skillCount 
					FROM (".SPEC_TBL_COMPONENTS." as co
					LEFT JOIN ".SPEC_TBL_SPEC." as spec
					ON spec.".SPEC_TBL_SPEC_ID." = co.".SPEC_TBL_COMPONENTS_SPEC_ID.")
					WHERE spec.".SPEC_TBL_SPEC_JOB_ID." = \"".$jobId."\" 
					AND spec.".SPEC_TBL_SPEC_LEVEL." = \"".$level."\" 
					AND co.".SPEC_TBL_COMPONENTS_TEMPLATE." = \"N\"
					GROUP BY co.".SPEC_TBL_COMPONENTS_SKILL_ID;
$result = $DB->RawQuery($abfrage);*/
//Show($result);
$jobs = array();

foreach ($result[3] as $row) {
		
	$job = array( 
					"id" => $row['id']
					,"name" => $row['name']
					,"level" => $row['level']
					,"mp" => $row['mp']
					,"hp" => $row['hp']
					,"tp" => $row['tp']
					,"cast" => $row['cast']
					,"recast" => $row['recast']
					,"range" => $row['range']
					,"icon" => $row['icon']
					,"desc" => $row['description']
					,"type" => "skill"
					,"skillCount" => $row['skillCount']
					,"skillName" => $row['skillName']
					,"totalCount" => $tCountresult[2][0]['totalCount']
				);
	array_push($jobs, $job);
}
echo json_encode($jobs);
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);
?>

